// formModal.js
function openModalWithDefaults(type, value_ls=null) {
    let modalElement, inputElement;
    if (type === 'addModal') {
        modalElement = document.getElementById('addFormModal');
        inputElement = document.getElementById('add_title');
    } else if (type === 'modifyModal') {
        modalElement = document.getElementById('modifyFormModal');
        inputElement = document.getElementById('old_title');
        inputElement.value = value_ls[0];
        inputElement2 = document.getElementById('modify_title');
        inputElement2.value = value_ls[0];
        inputElement3 = document.getElementById('modify_sub_title');
        inputElement3.value = value_ls[1];
    } else if (type === 'deleteModal') {
        modalElement = document.getElementById('deleteFormModal');
        inputElement = document.getElementById('delete_title');
        inputElement.value = value_ls[0];
        inputElement2 = document.getElementById('delete_sub_title');
        inputElement2.value = value_ls[1];
    } else if (type === 'refreshData') {
        modalElement = document.getElementById('refreshDataModal');
    } else {
        throw "错误的type";
    }

    // 将 type 存储在模态框的 data-type 属性中
    modalElement.dataset.type = type;

    // 实例化并显示模态框
    const modalInstance = new bootstrap.Modal(modalElement);
    modalInstance.show();
}


function submitForm(formId) {
    // 获取表单元素
    var form = document.getElementById(formId);
    console.log("即将刷新数据", formId, form)
//    const csrfToken = "{{ csrf_token }}";
    let url = '';
    let formData = null;
    if (formId=="refreshData") {
        // 根据模态框内容获取数据
        formData = {
            type: formId
        };
        url = `/index/lab/refresh_data/`;
    }else if (formId=="deleteForm") {
        // 根据模态框内容获取数据
        formData = {
            type: formId,
            title: form.elements['delete_title'].value
        };
        url = `/index/lab/del_model/`;
    }else if (formId=="modifyForm") {
        // 根据模态框内容获取数据
        formData = {
            type: formId,
            old_title: form.elements['old_title'].value,
            title: form.elements['modify_title'].value,
            sub_title: form.elements['modify_sub_title'].value
        };
        url = `/index/lab/modify_model/`;
    }else if (formId=="addForm") {
        // 根据模态框内容获取数据
        formData = {
            type: formId,
            title: form.elements['add_title'].value,
            sub_title: form.elements['add_sub_title'].value,
        };
        url = `/index/lab/add_model/`;
    }


    // 发起包含 type 和表单数据的请求
    fetch(url, {
        method: 'POST',  // 如果需要 GET 请求
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrfToken
        },
        body: JSON.stringify(formData)
    })
    .then(response => response.json())
    .then(data => {
        console.log(data);
        if ("model_data" in data) {
            form.reset(); // 清空表单
            const modalElement = document.getElementById(formId+"Modal");
            const modalInstance = bootstrap.Modal.getInstance(modalElement);
            modalInstance.hide(); // 关闭模态框
            // 重新执行进入页面时的数据加载动作
            window.render_list_page(data);
            window.render_module_page(data);
        }else if(data.success) {
            form.reset(); // 清空表单
            const modalElement = document.getElementById(formId+"Modal");
            const modalInstance = bootstrap.Modal.getInstance(modalElement);
            modalInstance.hide(); // 关闭模态框
        }else {
            console.log(data)
            alert('提交失败，请重试');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('提交失败，请重试');
    });
}




